package com.weipai.demo.controller;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.service.LoansManagerService;
import com.weipai.demo.service.UserService;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: gusl
 * @Date: 2019/5/31 10:33
 * @Description:用户模块
 */
@RestController
public class UserController {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private UserService userService;
    @Autowired
    private LoansManagerService loansManagerService;


    /**
     *  登录
     *
     * @author gusl
     * @date 2019/5/31 10:34
     * @param  [username, password, request]
     * @return com.weipai.springboot.model.api.Response
     *
     */

    @RequestMapping(value = "/user/login")
    public Response login(String userName, String password, int type, HttpServletRequest request) {
        logger.info("/user/login:username:"+userName+",password:"+password+",source:"+type);
        return userService.login(userName, password, type);
    }

    /**
     * 用户注册                            管理员
     *
     * @param mobile：手机号
     * @param newCode：输入的验证码
     * @param oldCode：第三方的验证码
     * @param password：密码
     * @return
     */
    @ResponseBody
    @RequestMapping("/user/register")
    public Response register(String applyforMobile,
                             String newCode,
                             String oldCode,
                             String password
                             ) {
        return loansManagerService.save(applyforMobile, newCode, oldCode, password);
    }


//    @PostMapping("/customer/register")
    @ResponseBody
    @RequestMapping("/user/register1")
    public Response register(String userName,
                             String password,
                             String email) {
        return userService.saveUser(userName, password, email);
    }

    //退出
    @RequestMapping(value = "/user/loginOut")
    public Response loginOut() {
        return userService.loginOut();
    }


    /**
     * 管理员重置密码
     *
     * @param oldPassword：旧密码
     * @param newPassword：新密码
     * @param twoPassword：新密码确认
     * @return
     */
    @RequestMapping(value = "/user/resetUserPassword")
    public Response resetUserPassword(@RequestParam(value = "oldPassword", required = false) String oldPassword,
                                       @RequestParam(value = "userId", required = false) String adminId,
                                       @RequestParam(value = "newPassword", required = false) String newPassword,
                                       @RequestParam(value = "twoPassword", required = false) String twoPassword) {
        return userService.resetUserPassword(adminId, oldPassword, newPassword, twoPassword);
    }
    /**
     * 未登录，shiro应重定向到登录界面，此处返回未登录状态信息由前端控制跳转页面
     * @return
     */
    @RequestMapping(value = "/unauth")
    public Response unauth() {
        Response result = new Response();
        result.setMsg(RetCode.NOT_LOGIN.getMsg());
        result.setCode(RetCode.NOT_LOGIN);
        return result;
    }

}
